package cn.topcodes.tcsf.admin.web.controller;

import cn.topcode.unicorn.utils.DateUtil;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.admin.domain.entity.SysUser;
import cn.topcodes.tcsf.admin.service.org.SysUserService;
import cn.topcodes.tcsf.admin.web.websocket.AdminWebsocketHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Unicorn on 2018/2/7.
 */
@Api(tags = {"测试接口"})
@RestController
@RequestMapping("/api/test")
public class TestRestApi {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private TestService testService;

    @ApiOperation("管理后台Websocket推送")
    @RequestMapping(value = "push-websocket", method = RequestMethod.POST)
    public ApiResponseEntity pushWebsocket(
            @RequestParam String username,
            @RequestParam String message
    ) {
        SysUser sysUser = sysUserService.findByUsername(username);
        Assert.notNull(sysUser, "用户不存在");
        AdminWebsocketHelper.pushCmd(sysUser.getId(), "CMD", message);
        return ApiResponseEntity.buildOK();
    }

    @ApiOperation("测试异步")
    @RequestMapping(value = "test-async", method = RequestMethod.GET)
    public ApiResponseEntity testAsync() {
        System.out.println("call method " + DateUtil.formatCurrentDate());
        testService.async();
        return ApiResponseEntity.buildOK();
    }
}
